<?php

declare(strict_types=1);

namespace App\Game\Listener;

use App\Fastapp\Listener\Event\AccountRechargeEvent;
use App\Fastapp\Listener\Event\AccountRegisterEvent;
use App\Game\Api\Service\AccountService;
use App\Game\Job\GradientJob;
use App\Game\Job\RechargeJob;
use App\Game\Job\TaskJob;
use App\Game\Listener\Event\BetEvent;
use App\Game\Listener\Event\TaskSevenDaySignEvent;
use App\Game\Model\GameAccountVip;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use function Hyperf\Support\class_basename;

#[Listener]
class GameListener implements ListenerInterface
{
    public function __construct(protected ContainerInterface $container)
    {
    }

    public function listen(): array
    {
        return [
            BetEvent::class,
            TaskSevenDaySignEvent::class,
            AccountRechargeEvent::class,
            AccountRegisterEvent::class,
        ];
    }

    public function process(object $event): void
    {
        if (!str_starts_with((string)$event->account_type, '7')) return;
        if ($event instanceof BetEvent) {
            AccountService::effectiveLevel($event->user_id, $event->account_type);
            fastapp_queue_push(new GradientJob($event->user_id, $event->data_id, class_basename($event), $event->account_type));
        } elseif ($event instanceof TaskSevenDaySignEvent) {
            fastapp_queue_push(new TaskJob($event->user_id, class_basename($event), $event->account_type));
        } elseif ($event instanceof AccountRechargeEvent) {
            fastapp_queue_push(new RechargeJob($event->user_id, $event->data_id, $event->account_type));
            fastapp_queue_push(new TaskJob($event->user_id, class_basename($event), $event->account_type, $event->data_id));
            fastapp_queue_push(new GradientJob($event->user_id, $event->data_id, class_basename($event), $event->account_type));
        } elseif ($event instanceof AccountRegisterEvent) {
            GameAccountVip::query()->create(['user_id' => $event->user_id]);
        }
    }
}
